package com.das.mechanic_base.adapter.alone;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.bean.alone.NewServiceHeaderBean;


import java.util.ArrayList;
import java.util.List;

public class X3NewAloneSerViceSetAdapter extends RecyclerView.Adapter<X3NewAloneSerViceSetAdapter.AloneServiceSetHolder> {

    private List<NewServiceHeaderBean.AllSerViceListBean> mList;

    private Context mContext;

    private List<Boolean> selects;

    private float percent;

    public X3NewAloneSerViceSetAdapter(Context mContext) {
        this.mContext = mContext;
        this.mList = new ArrayList<>();
        this.selects = new ArrayList<>();
    }

    /**
     * 动态更改布局大小
     * @param percent
     */
    public void changePercent(float percent) {
        if (this.percent == percent) {
            return;
        }
        this.percent = percent;
        notifyDataSetChanged();
    }

    public void changeServiceSet(List<NewServiceHeaderBean.AllSerViceListBean> mList) {
        this.mList = mList;
        selects = new ArrayList<>();
        for (int i = 0; i < mList.size(); i++) {
            selects.add(i == 0);
        }
        notifyDataSetChanged();
    }

    public List<NewServiceHeaderBean.AllSerViceListBean.ListBeanX> getSelectAloneService() {
        int index = 0;
        for (int i = 0; i < selects.size(); i++) {
            if (!selects.get(i)) {
                continue;
            }
            index = i;
            break;
        }
        return mList.get(index).list;
    }

    @NonNull
    @Override
    public AloneServiceSetHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new AloneServiceSetHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_alone_service_set_item,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull AloneServiceSetHolder holder, int position) {
        if (selects.get(position)) {
            holder.tv_select.setTextColor(ContextCompat.getColor(mContext,R.color.bg_313340));
            holder.view.setVisibility(View.VISIBLE);
            //   selectImageIcon(holder,mList.get(position).getServiceCategoryName());
        }else {
            holder.tv_select.setTextColor(ContextCompat.getColor(mContext,R.color.bg_707380));
            holder.view.setVisibility(View.GONE);
        }
        holder.tv_select.setText(mList.get(position).serviceCategoryName + "");

        holder.itemView.setOnClickListener(view -> {
            for (int i = 0; i < mList.size(); i++) {
                selects.remove(i);
                selects.add(i,i == position);
            }

            if (iOnClick != null) {
                iOnClick.iOnClickItem(position);
            }
            notifyDataSetChanged();
        });

    }



    IOnClick iOnClick;

    public void setiOnClick(IOnClick iOnClick) {
        this.iOnClick = iOnClick;
    }

    public interface IOnClick {
        void iOnClickItem(int position);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class AloneServiceSetHolder extends RecyclerView.ViewHolder {
        TextView tv_select;
        ImageView iv_icon;
        RelativeLayout rl_item;
        View view;
        public AloneServiceSetHolder(@NonNull View itemView) {
            super(itemView);
            tv_select = itemView.findViewById(R.id.tv_select);
            iv_icon = itemView.findViewById(R.id.iv_icon);
            rl_item = itemView.findViewById(R.id.rl_item);
            view = itemView.findViewById(R.id.view);
        }
    }
}